package cumeo89.chii.client.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

import cumeo89.chii.client.util.Action;
import cumeo89.chii.client.util.MyChangeListener;

public class Palette extends Composite {

	private TrivialPanel pnlMain = new TrivialPanel("div");
	private TrivialPanel lblHeader = new TrivialPanel("div");
	private TrivialPanel pnlBody = new TrivialPanel("div");

	private ArrayList<Item> items = new ArrayList<Item>();
	private Item selectedItem = null;
	private boolean supressRefresh = false;

	public Palette() {
		lblHeader.setHTML("Mục&nbsp;mới");
		lblHeader.setStyleName("palette-header");
		lblHeader.setWidth("100%");
		pnlMain.add(lblHeader);

		pnlBody.setStyleName("palette-body");
		pnlBody.setWidth("100%");
		pnlMain.add(pnlBody);

		pnlMain.setStyleName("palette");
		initWidget(pnlMain);
	}

	public void addItem(String text, Action action) {
		items.add(new Item(text, action));
		refresh();
	}

	public void removeItem(int i) {
		items.remove(i);
		refresh();
	}

	public String getItemText(int i) {
		return items.get(i).getText();
	}

	public Command getItemCommand(int i) {
		return items.get(i).getAction();
	}

	public int getItemCount() {
		return pnlBody.getWidgetCount();
	}

	public void onClick(Widget sender) {
		selectedItem = (Item) sender;
		selectedItem.getAction().execute();
	}

	public boolean isSupressRefresh() {
		return supressRefresh;
	}
	
	public void setSupressRefresh(boolean supressRefresh) {
		this.supressRefresh = supressRefresh;
		if (!supressRefresh) {
			refresh();
		}
	}
	
	private void refresh() {
		if (supressRefresh) {
			return;
		}
		Collections.sort(items, new Comparator<Item>() {

			public int compare(Item a, Item b) {
				if (a.getAction().isEnabled() != b.getAction().isEnabled()) {
					return a.getAction().isEnabled() ? 1 : -1;
				}
				return a.getText().compareToIgnoreCase(b.getText());
			}
		});
		for (Item item : items) {
			pnlBody.add(item);
		}
	}

	private class Item extends TrivialPanel implements MyChangeListener {

		private Action action;

		public Item(String text, Action action) {
			super("div", text);
			this.action = action;
			this.setStyleName("palette-item");
			action.addChangeListener(this);
			sinkEvents(Event.ONCLICK);
		}

		public Action getAction() {
			return action;
		}

		public String getText() {
			return getElement().getInnerText();
		}

		@Override
		public void onBrowserEvent(Event event) {
			if (event.getTypeInt() == Event.ONCLICK) {
				onClick(this);
			}
		}

		public void onChanged(Object sender) {
			setStyleName(action.isEnabled() ? "palette-item"
					: "palette-item-disabled");
			refresh();
		}

	}

}
